<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>随鼠标转动</title>
    <style>
       body{
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: #ed863d;
        }
        .box{
            display: grid;
            grid-template-columns: repeat(7,40px);
            grid-template-rows: repeat(7,40px);
            grid-gap: 1.5rem;
        }
        .item{
            background: black;
            border-radius: 5px;
            border-left: solid 10px white;
            position: relative;
        }
        .item::after,.item::before{
            content:'';
            width: 5px;
            height: 5px;
            background: rgb(221, 26, 26);
            display: block;
            position: absolute;
            border-radius: 50%;
            right: 0;
        }
        
        .item::after{
            top:25px;
        }
        .item::before{
          bottom: 25px;
        }
    </style>
</head>

<body>
   <div class="box">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
   </div>
   <script>
        document.addEventListener("mousemove",(e)=>{
            // 鼠标位置
            let mouseX = e.x;
            let mouseY = e.y;
            // 所有item
            let items = document.querySelectorAll(".item");
            items.forEach(item =>{
                // 距离左边和顶部的距离
            let offsetX = item.offsetLeft;
            let offsetY = item.offsetTop;
            console.log(offsetX,offsetY)
            
            //计算以item为原点鼠标在x,y轴上的位置
            let diffX = mouseX - offsetX;
            let diffY = mouseY - offsetY;
            //   求出item旋转角度
            //1° = Math.pI /180

            let hudu = Math.atan2(diffY,diffX);
            let angle = hudu / (Math.PI / 180);
            // 设置item旋转
            item.style.transform = `rotate(${angle}deg)`
            })
        })
   </script>
</body>

</html>